package org.example.lys.utils;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ; lidongdong
 * @Description html处理工具
 * @Date 2020-10-28
 */
public class HtmlUtils {


    /**
     * HTML格式文本转换为纯文本
     *
     * @param html HTML格式文本
     * @return
     */
    public static String convert(String html) {
        if (StringUtils.isEmpty(html)) {
            return "";
        }

        Document document = Jsoup.parse(html);
        Document.OutputSettings outputSettings = new Document.OutputSettings().prettyPrint(false);
        document.outputSettings(outputSettings);
        document.select("br").append("\\n");
        document.select("p").prepend("\\n");
        document.select("p").append("\\n");
        String newHtml = document.html().replaceAll("\\\\n", "");
        newHtml = newHtml.replaceAll("↵↵", "");
        String plainText = Jsoup.clean(newHtml, "", Whitelist.none(), outputSettings);
        String result = StringEscapeUtils.unescapeHtml3(plainText.trim());
        return result;
    }

    /**
     * HTML格式文本提取图片
     *
     * @param html HTML格式文本
     * @return
     */
    public static List<String> convertImage(String html) {
        List<String> result = new ArrayList<>();
        //解析html
        Document doc = Jsoup.parse(html);
        //获取所有img标签
        Elements imgList = doc.body().getElementsByTag("img");
        for (Element element : imgList) {
            //获取src属性值
            String src = element.attr("src");
            result.add(src);
        }

        return result;
    }


    public static void main(String[] args) {
        String html = "<p style=\"margin-left:0cm\">&nbsp;</p>↵↵<p style=\"margin-left:0cm\">&nbsp;</p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#f33b45\">1、根据关键字查询：</span></p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#000000\">#</span><span style=\"color:#000000\">查询</span><span style=\"color:#000000\">catalina.out</span><span style=\"color:#000000\">日志文件中的关键词为</span><span style=\"color:#000000\">2016-04-13 11:26:00</span><span style=\"color:#000000\">的日志信息</span></p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#000000\">grep -C 10 &#39;2016-04-13 11:26:00&#39; catalina.out |more </span><span style=\"color:#000000\">或者</span></p>↵↵<p style=\"margin-left:0cm\">&nbsp;<span style=\"color:#000000\">从前往后</span><span style=\"color:#000000\">&nbsp; cat /logs/web-android/webAndroid.log |grep &#39;createOrder&#39; |more</span></p>↵↵<p style=\"margin-left:0cm\">&nbsp;<span style=\"color:#000000\">从后往前</span><span style=\"color:#000000\">&nbsp; tac /logs/web-android/webAndroid.log |grep &#39;createOrder&#39; |more</span></p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#000000\">解释：</span><br />↵<span style=\"color:#000000\">grep</span>&nbsp;<span style=\"color:#000000\">：查询，筛选</span><br />↵<span style=\"color:#000000\">-C</span>&nbsp;<span style=\"color:#000000\">：</span><span style=\"color:#000000\"> grep</span><span style=\"color:#000000\">的</span><span style=\"color:#000000\">-A, -B, -C</span><span style=\"color:#000000\">选项分别可以显示匹配行的后</span><span style=\"color:#000000\">,</span><span style=\"color:#000000\">前</span><span style=\"color:#000000\">,</span><span style=\"color:#000000\">后前多少行内容</span><span style=\"color:#000000\">:</span><br />↵<span style=\"color:#000000\">10</span>&nbsp;<span style=\"color:#000000\">：是前后多少行</span><br />↵<span style=\"color:#000000\">&#39;2016-04-13 11:26:00&#39;</span>&nbsp;<span style=\"color:#000000\">：关键词</span><span style=\"color:#000000\">,</span><span style=\"color:#000000\">注意是单引号包裹</span><br />↵<span style=\"color:#000000\">catalina.out</span>&nbsp;<span style=\"color:#000000\">：</span> <span style=\"color:#000000\">检索的文件</span> <span style=\"color:#000000\">可以是目录</span>&nbsp;<span style=\"color:#000000\">./</span></p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#000000\">|more</span>&nbsp;<span style=\"color:#000000\">：</span> <span style=\"color:#000000\">这个是以分页的形式查看，整屏太多，看不完，需要你慢慢的静下心来一点一点一滴一滴的看才能看出问题的所在，就像这篇文章一样，必须要静下心来，轻轻的点个赞再走。</span></p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#000000\">文章转载自：</span><span style=\"color:#000000\">https://www.cnblogs.com/ae6623/p/5395302.html</span></p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#f33b45\">2、根据时间范围查找：</span></p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#000000\">sed -n &#39;/2018-10-09 00:00:01/,/2018-10-09 00:00:01/p&#39; &nbsp;/logs/service-free/serviceFree.log&nbsp;</span></p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#f33b45\">3、高亮显示关键字：</span></p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#000000\">tail -f /logs/web-h5/webH5.log | egrep &quot;LoginFilter|param&quot; -A10&nbsp;-B10 --color=auto</span></p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#000000\">​<img alt=\"\" class=\"has\" src=\"https://img-blog.csdn.net/2018101611352847?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI3Mzc2ODcx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70\" /></span></p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#000000\">egrep = grep -E </span><span style=\"color:#000000\">是扩展</span><span style=\"color:#000000\">grep</span><span style=\"color:#000000\">命令；</span><span style=\"color:#000000\">&nbsp;<br />↵-A10 -B10</span><span style=\"color:#000000\">表示在有关键词的上下</span><span style=\"color:#000000\">10</span><span style=\"color:#000000\">行范围内都进行过滤关键词的操作，英文解释：</span><span style=\"color:#000000\">&nbsp;<br />↵*Context control:&nbsp;<br />↵-B, &ndash;before-context=NUM print NUM lines of leading context&nbsp;<br />↵-A, &ndash;after-context=NUM print NUM lines of trailing context*&nbsp;</span><br />↵<span style=\"color:#000000\">尝试</span><span style=\"color:#000000\"> -A1 -B1</span><span style=\"color:#000000\">得到结果和原命令基本一致</span>&nbsp;<br />↵<span style=\"color:#000000\">尝试去掉</span><span style=\"color:#000000\"> -A -B</span><span style=\"color:#000000\">参数，得到结果和原命令基本一致</span>&nbsp;</p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#000000\">即：</span><span style=\"color:#000000\">tail -f /logs/web-h5/webH5.log | egrep &quot;LoginFilter|param&quot; &nbsp;--color=auto<br />↵&ndash;color=auto </span><span style=\"color:#000000\">来将关键字部分使用颜色显示，至于如何自定义关键词的颜色，这个。。。就比较复杂了，请参考文章《在</span><span style=\"color:#000000\">linux</span><span style=\"color:#000000\">下给</span><span style=\"color:#000000\">grep</span><span style=\"color:#000000\">命令添加颜色</span><span style=\"color:#000000\">&nbsp;<a href=\"http://blog.csdn.net/diy534/article/details/7025640\"><span style=\"color:#000000\">http://blog.csdn.net/diy534/article/details/7025640</span></a></span><span style=\"color:#000000\">》</span></p>↵↵<p style=\"margin-left:0cm\"><span style=\"color:#000000\">文章参考自：</span><span style=\"color:#000000\"><a href=\"https://blog.csdn.net/kuzaman/article/details/50989335\"><span style=\"color:#000000\">https://blog.csdn.net/kuzaman/article/details/50989335</span></a></span></p>↵↵<h1>4、mac 查看端口 关闭端口</h1>↵↵<p>$ lsof -i:9003</p>↵↵<p style=\"margin-left:0cm\"><img alt=\"\" class=\"has\" src=\"https://img-blog.csdn.net/20170923102509872?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ195YW5nMTk5NA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast\" /></p>↵↵<p><strong>由上面命令可以肯定具体的进程，然后杀死进程</strong></p>↵↵<pre class=\"has\">↵<code>kill -9 10307</code></pre>↵↵<p>&nbsp;</p>↵↵<p>测试网络：</p>↵↵<p>0、wget</p>↵↵<p>wget ip:port，测试端口是否可用</p>↵↵<p>也可以用于get和post请求</p>↵↵<p>get方法请求 ，wget &quot;<a href=\"http://blog.csdn.net/alanwei04/article/details/79313029\">http://blog.csdn.net/alanwei04/article/details/79313029</a>&quot;</p>↵↵<p>post访求语法，wget --post-data=&quot;param=value&quot; <a href=\"http://xxx.xx\">http://xxx.xx</a></p>↵↵<p>&nbsp;</p>↵↵<p>1、curl</p>↵↵<p>get方法请求，curl &quot;<a href=\"http://blog.csdn.net/alanwei04/article/details/79313029\">http://blog.csdn.net/alanwei04/article/details/79313029</a>&quot;</p>↵↵<p>post方法请求，curl -d &quot;param1=value1&amp;param2=value2&quot; &quot;<a href=\"http://xxxx.xxx\">http://xxxx.xxx</a>&quot;</p>↵↵<p>&nbsp;</p>↵↵<p>&nbsp;</p>↵";
        List<String> list1 = convertImage(html);
        for (String str : list1) {
            System.out.println(str);
        }
        System.out.println(convert(html).substring(0,200000));
    }
}
